from django.conf import settings
from urllib.parse import urlencode, parse_qs
import json
import requests


class OAuthweibo(object):
    """
    weibo认证辅助工具类
    """

    def __init__(self, client_id=None, client_secret=None, redirect_uri=None, state=None):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect_uri = redirect_uri
        self.state = state   # 用于保存登录成功后的跳转页面路径

    def get_weibo_url(self):
        # weibo登录url参数组建
        data_dict = {
            'response_type': 'code',
            'client_id': self.client_id,
            'redirect_uri': self.redirect_uri,
            'state': self.state
        }

        # 构建url
        #https://api.weibo.com/oauth2/authorize
        weibo_url = 'https://api.weibo.com/oauth2/authorize?' + urlencode(data_dict)
        # http://open.weibo.com/wiki/Oauth2/authorize

        return weibo_url

    # 获取access_token值
    def post_access_token(self, code):
        # 构建参数数据
        data_dict = {

            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': 'authorization_code',
            'redirect_uri': self.redirect_uri,
            'code': code

            # 'state': self.state
        }
        # 'https://api.weibo.com/oauth2/access_token?client_id=3305669385&client_secret=74c7bea69d5fc64f5c3b80c802325276&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fwww.meiduo.site%3A8080%2Fsina_callback.html&code=b179944707ff0c0f1ea4f5b027b2acba'
        # https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
        # 'fde5b205f5feeb85d341cd0d88837e15'
        # 'b179944707ff0c0f1ea4f5b027b2acba'
        # 构建url
        access_url = 'https://api.weibo.com/oauth2/access_token?' + urlencode(data_dict)

        # 发送请求
        try:
            response = requests.post(access_url)

            # 提取数据
            # access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14
            # data = response
            access_token = eval(response.content.decode()).get('access_token')
            # 转化为字典
            # data = parse_qs(data)
        except:
            raise Exception('微薄请求失败')

        # 提取access_token
        # access_token = data.get('access_token', None)

        if not access_token:
            raise Exception('access_token获取失败')

        return access_token

        # 发送请求


        # url = 'http://api.weibo.com/Oauth2/access_token'
        # try:
        #     response = requests.post('http://api.weibo.com/Oauth2/access_token',data_dict)
        #     data = response.text
        #     data_dict=json.loads(data)
        #     access_token = data_dict['access_token']
        # except:
        #     raise Exception('weibo请求失败')
        #
        # return access_token

        # response = requests.post('http://api.weibo.com/Oauth2/access_token',data_dict)

        # data = response.text

        #返回的数据解码变成字典
        # result = json.loads(data)
        #获取access_token
        # self.access_token = result['access_token']

        # return self.access_token
